﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client.Exceptions
{
    public partial class ExceptionAdminFrm : FormBase, IModelInfo
    {
        public ExceptionAdminFrm()
        {
            InitializeComponent();
        }

        private DisplayTypes _DisplayType = DisplayTypes.TabPage;

        public DisplayTypes DisplayType
        {
            get
            {
                return _DisplayType;
            }
            set
            {
                _DisplayType = value;
            }
        }

        private DataTable DTException = new DataTable(BaseExceptionTable.TableName);
        private bool PermissionAccess;
        private bool PermissionDelete;
        private bool PermissionExport;

        public override string ModelCode
        {
            get
            {
                return this.GetType().FullName;
            }
        }

        public override string ModelName
        {
            get
            {
                return this.Text;
            }
        }

        public override void StartModel()
        {
            this.ShowDialog();
        }

        public IList<PermissionInfo> Permissions
        {
            get
            {
                return new List<PermissionInfo>() {
               new PermissionInfo("ExceptionAdminFrm.Access", "异常管理") ,
                    new PermissionInfo("ExceptionAdminFrm.Delete", "删除异常") ,
                        new PermissionInfo("ExceptionAdminFrm.Export", "导出异常")
        };
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in this.DTException.Rows)
            {
                row[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in this.DTException.Rows)
            {
                bool flag = true;
                if (row[BaseBusinessLogic.SelectedColumn].ToString() == flag.ToString())
                {
                    row[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    row[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            base.ExportExcel(this.grdExceptionAdmin, @"\Modules\Export\", "异常信息.csv");
        }

        private void btnBatchDelete_Click(object sender, EventArgs e)
        {
            if (this.CheckInputBatchDelete() && (XtraMessageBox.Show(AppMessage.MSG0015, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK))
            {
                this.BatchDelete();
            }
        }

        private void btnClearException_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("您确认清除吗？", AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                this.ClearException();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private bool CheckInputBatchDelete()
        {
            return BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTException, BaseBusinessLogic.SelectedColumn);
        }

        private void BatchDelete()
        {
            this.Cursor = Cursors.WaitCursor;
            this.DTException = ServiceManager.Instance.ExceptionService.BatchDelete(base.UserInfo, this.GetSelecteIDs());
            this.BindData();
            this.Cursor = Cursors.Default;
        }

        private string[] GetSelecteIDs()
        {
            return BaseInterfaceLogic.GetSelecteIDs(this.DTException, BaseExceptionTable.FieldID, BaseBusinessLogic.SelectedColumn);
        }

        private void BindData()
        {
            this.DataTableAddColumn();
            this.grdExceptionAdmin.DataSource = this.DTException.DefaultView;
            this.grdExceptionAdmin.Refresh();
            if (!this.PermissionDelete)
            {
                if (this.colCheckBox.ColumnEdit != null)
                {
                    this.colCheckBox.ColumnEdit.ReadOnly = !this.PermissionDelete;
                }

                this.btnSelectAll.Enabled = false;
                this.btnInvertSelect.Enabled = false;
            }
            this.SetControlState();
        }

        private void SetControlState()
        {
            this.btnSelectAll.Enabled = false;
            this.btnInvertSelect.Enabled = false;
            this.btnExport.Enabled = false;
            this.btnClearException.Enabled = false;
            this.btnBatchDelete.Enabled = false;
            if (this.DTException.DefaultView.Count > 0)
            {
                this.btnSelectAll.Enabled = this.PermissionExport || this.PermissionExport;
                this.btnInvertSelect.Enabled = this.PermissionExport || this.PermissionExport;
                this.btnExport.Enabled = this.PermissionExport;
                this.btnClearException.Enabled = this.PermissionDelete;
                this.btnBatchDelete.Enabled = this.PermissionDelete;
            }
        }

        private void ClearException()
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.DTException = ServiceManager.Instance.ExceptionService.Truncate(base.UserInfo);
                this.BindData();

                XtraMessageBox.Show("清除成功。", AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception exception)
            {
                BaseInterfaceLogic.WriteException(base.UserInfo, exception);
                new FrmException(base.UserInfo, exception).ShowDialog();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void DataTableAddColumn()
        {
            BaseInterfaceLogic.DataTableAddColumn(this.DTException, BaseBusinessLogic.SelectedColumn);
        }

        private void ExceptionAdminFrm_Load(object sender, EventArgs e)
        {
            this.FormOnLoad();
        }

        private void FormOnLoad()
        {
            this.Cursor = Cursors.WaitCursor;
            this.DTException = ServiceManager.Instance.ExceptionService.GetList(base.UserInfo);
            base.Localization(this);
            this.GetPermission();
            this.BindData();
            this.Cursor = Cursors.Default;
        }

        private void GetPermission()
        {
            this.PermissionAccess = base.UserInfo.IsAdministrator;
            this.PermissionDelete = base.UserInfo.IsAdministrator;
            this.PermissionExport = base.UserInfo.IsAdministrator;
        }

        private void ExceptionAdminFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 0x74)
            {
                this.FormOnLoad();
            }
        }

        private void grdExceptionAdminView_DoubleClick(object sender, EventArgs e)
        {
            DataRow dataGridViewEntity = BaseInterfaceLogic.GetDataGridViewEntity(this.grdExceptionAdminView);
            if (dataGridViewEntity != null)
            {
                BaseExceptionEntity exceptionEntity = new BaseExceptionEntity(dataGridViewEntity);
                new FrmException(exceptionEntity).ShowDialog();
            }
        }
    }
}